package com.auto.learning.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.bean.ResponeResult;
import com.auto.learning.event.Handle;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.service.MyUserInfo;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.SharedPreferencesUtils;

public class CompanyRecommendActivity extends Activity  {
	
	private final static int UNBIND_SUCCESS = 1;
	private ImageView activity_base_back;
	private TextView tv_company_bind;
	private EditText et_company_code;
	private ImageView image_bg;
	
	private Handle handler = new Handle(this){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HttpRequestHelper.GETDATA_SUCCESS:
				try {
					ResponeResult responeResult = (ResponeResult) msg.obj;
					if (responeResult != null) {
						CommonMethod.makeNotice(CompanyRecommendActivity.this, "绑定成功");
						JSONObject jsonObject = new JSONObject(responeResult.getData());
						String companyName = jsonObject.getString("companyName");
						String code = jsonObject.getString("code");
						SharedPreferencesUtils.setParam(CompanyRecommendActivity.this, "companyCode_" + SharedPreferencesUtils.getParam(CompanyRecommendActivity.this, HttpRequestHelper.USERID, 0), code);
						MyUserInfo.getInstance().getUserInfoBean().setCompanyName(companyName);
						setResult(212);
						finish();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case HttpRequestHelper.GETDATA_FAIL:
				CommonMethod.makeNotice(CompanyRecommendActivity.this, msg.obj.toString());
				break;
			case UNBIND_SUCCESS:
				CommonMethod.makeNotice(CompanyRecommendActivity.this, "解除绑定成功");
				SharedPreferencesUtils.setParam(CompanyRecommendActivity.this, "companyCode_" + SharedPreferencesUtils.getParam(CompanyRecommendActivity.this, HttpRequestHelper.USERID, 0), "");
				MyUserInfo.getInstance().getUserInfoBean().setCompanyName("");
				setResult(212);
				finish();
				break;
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_recommend);
		initView();
		TemporaryVariable.activities.push(this);
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		TemporaryVariable.activities.removeElement(this);
		super.onDestroy();
	}
	
	private void initView(){
		image_bg = (ImageView) findViewById(R.id.image_bg);
		image_bg.setImageBitmap(CommonMethod.readBitMap(this, R.drawable.login_bg));
		et_company_code = (EditText) findViewById(R.id.et_company_code);
		tv_company_bind = (TextView) findViewById(R.id.tv_company_bind);
		activity_base_back = (ImageView) findViewById(R.id.activity_base_back);
		activity_base_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		tv_company_bind.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (TextUtils.isEmpty(MyUserInfo.getInstance().getUserInfoBean().getCompanyName())) {
					String code = et_company_code.getText().toString();
					if (TextUtils.isEmpty(code)) {
						CommonMethod.makeNotice(CompanyRecommendActivity.this, "专享码不能为空");
						return;
					} else {
						bindCompanyCode(code);
					}
				}else {
					unBindCompanyCode();
				}
			}
		});
		
		String name = MyUserInfo.getInstance().getUserInfoBean().getCompanyName();
		String code = (String) SharedPreferencesUtils.getParam(CompanyRecommendActivity.this, "companyCode_" + SharedPreferencesUtils.getParam(CompanyRecommendActivity.this, HttpRequestHelper.USERID, 0), "");
		if (!TextUtils.isEmpty(name)) {
			tv_company_bind.setText("解除绑定");
			et_company_code.setText(code);
			et_company_code.setEnabled(false);
		}else {
			tv_company_bind.setText("绑定");
			et_company_code.setEnabled(true);
		}
	}
	
	private void bindCompanyCode(String code){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(HttpRequestHelper.USERID, String.valueOf(SharedPreferencesUtils.getParam(this, HttpRequestHelper.USERID, 0))));
		params.add(new BasicNameValuePair("code", code));
		HttpRequestHelper.executeHttpData(CompanyRecommendActivity.this, HttpRequestHelper.INVITE_CODE, params, handler, HttpRequestHelper.GETDATA_SUCCESS, HttpRequestHelper.GETDATA_FAIL);
	}
	
	private void unBindCompanyCode(){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(HttpRequestHelper.USERID, String.valueOf(SharedPreferencesUtils.getParam(this, HttpRequestHelper.USERID, 0))));
		HttpRequestHelper.executeHttpDataNoData(CompanyRecommendActivity.this, HttpRequestHelper.INVITE_CODE_CANCEL, params, handler, UNBIND_SUCCESS, HttpRequestHelper.GETDATA_FAIL);
	}
	
}
